<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 奖品管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" class="fr" @click="addVisible = true">添加奖项</el-button>
      </div>
      <el-table
        :data="awardData"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID" sortable width="70" align="center"></el-table-column>
        <el-table-column prop="prize_id" label="奖项" width="150" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="150" align="center"></el-table-column>
        <el-table-column label="图片" width="150" align="center">
          <template slot-scope="scope">
            <img style="width:100%" :src="scope.row.picture" alt>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="添加时间" align="center"></el-table-column>
        <el-table-column label="是否启用" width="120" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="setStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="editForm" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <!-- <el-input v-model="addForm.picture"></el-input> -->
          <el-upload
            class="upload-demo"
            ref="upload"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :on-change="handleChange"
            :auto-upload="false"
            :limit="1"
            :on-exceed="setFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModel">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="addForm" label-width="100px">
        <el-form-item label="奖项ID">
          <el-select v-model="addForm.prize_id" placeholder="选择奖项" class="handle-select mr10">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item,index) in prizeData"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <!-- <el-input v-model="addForm.picture"></el-input> -->
          <el-upload
            class="upload-demo"
            ref="upload"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :on-change="handleChange"
            :auto-upload="false"
            :limit="1"
            :on-exceed="setFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAward">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" top='30vh' center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/userApi.js";
export default {
  name: 'basetable',
  data() {
    return {
      url: './static/vuetable.json',
      awardData: [],
      prizeData: [],//奖项列表(用于修改删除时选择下拉框)
      editVisible: false,
      delVisible: false,
      addVisible: false,
      addForm: {
        prize_id: '',
        name: '',
        picture: ''
      },
      editForm:{
        id:'',
        prize_id: '',
        name: '',
        picture: ''
      },
      idx: -1,
      setId:'', //临时存放ID
      fileList: []
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.addForm.picture = ''
    },
    handlePreview(file) {
      console.log(file);
    },
    handleChange(file, fileList) {
      // 转化为base 64
      let reader = new FileReader()
      reader.onload = () => {
        this.addForm.picture = reader.result
        this.editForm.picture = reader.result
      }
      reader.readAsDataURL(file.raw)
      
      console.log(this.editForm);
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    getData() {
      api.getAwardList()
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.awardData = res.data.list;
          this.awardData.forEach(d => {
            if (d.status == 0) {
              d.status = true
            } else {
              d.status = false
            }
          })
        })
      api.getPrizeList()
        .then((res) => {
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.prizeData = res.data.list;
        })
    },
    setStatus(row) {
      const id = row.id
      api.statusAward({
        id: id
      })
        .then((res) => {
          if (res.code) {
            this.$message.error('服务器繁忙')
            return;
          }
          this.$message.success('修改成功')
          console.log(res);
          if (res.data.status == 0) {
            row.status = true
          } else {
            row.status = false
          }
        })
    },
    handleEdit(index, row) {
      this.editIndex = index
      const data = row
      this.editForm = {
        id: data.id,
        prize_id: data.prize_id,
        name: data.name,
        picture: ''
      }
      this.fileList = [{
        name:data.name,
        url:data.picture
      }]
      this.editVisible = true;
    },
    handleDelete(index, row) {
      this.idx = index;
      this.setId = row.id;
      this.delVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 保存编辑
    saveEdit() {
     api.editAward(this.editForm)
        .then((res) => {
          console.log(res);
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.getData()
          this.editVisible = false;
          this.$message.success('修改成功');
        })
    },
    // 确定删除
    deleteRow() {
      api.deleteAward({
        id: this.setId
      }).then(res => {
        if (res.code) {
          this.$message.error(res.message)
          return;
        }
        this.$message.success('删除成功')
        this.awardData.splice(this.idx, 1);
        this.delVisible = false;
      })
    },
    addAward() {
      const data = this.addForm;
      api.addAward(data)
        .then((res) => {
          console.log(res);
          if (res.code) {
            this.$message.error(res.message)
            return;
          }
          this.getData()
          this.$message.success('添加成功');
          this.addVisible = false
          this.addForm = {}
        })
    },
    setFileList(files, fileList) {
      this.fileList = fileList
      this.$forceUpdate()
      // this.$message.warning('最多只允许上传一个')
    },
    closeModel(){
      this.editVisible = false;
      this.fileList = []
      console.log(this.fileList);
    }
  }
}

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.el-upload-list--picture .el-upload-list__item-thumbnail {
  width: 100px !important;
  height: auto !important;
}
</style>
